/*
* Copyright (C) Microsoft. All rights reserved.  
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not 
* use this file except in compliance with the License.  You may obtain a copy 
* of the License at http://www.apache.org/licenses/LICENSE-2.0  
* 
* THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED 
* WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE, 
* MERCHANTABLITY OR NON-INFRINGEMENT. 
*
* See the Apache License, Version 2.0 for specific language governing 
* permissions and limitations under the License.
*
*/
#pragma once

#include <string>
#include <vector>
#include <memory>
#include <baja/db/db.h>
#include <journal/models/db/table_interfaces.h>

namespace journal { namespace model { namespace db
{

namespace b = baja;

class common_table : public icommon_table
{
public:
    common_table(std::weak_ptr<icommon_table_client> _client, std::shared_ptr<s::iengine> dbEngine, std::wstring name);
    static std::shared_ptr<icommon_table> create(std::weak_ptr<icommon_table_client> _client, std::shared_ptr<s::iengine> dbEngine, std::wstring name);

    // icommon_table
    void initialize();
    std::wstring name();
    std::shared_ptr<s::iengine> dbEngine();
    std::shared_ptr<s::itable> tableDefinition();
    std::shared_ptr<std::vector<std::shared_ptr<s::icell>>> stagingRow();
    std::shared_ptr<s::iindex> primaryIndex();

    unsigned int getNextId();

    void insert(std::shared_ptr<std::vector<std::shared_ptr<s::icell>>> stagingRow);

    void update(std::shared_ptr<std::vector<std::shared_ptr<s::icell>>> stagingRow,
                b::uint32 criteriaCount);

    void read(std::shared_ptr<std::vector<std::shared_ptr<s::icell>>> stagingRow, 
              b::uint32 criteriaCount,
              std::shared_ptr<s::iresult> result);

    void read(std::shared_ptr<s::iindex> keyIndex,
              std::shared_ptr<std::vector<std::shared_ptr<s::icell>>> stagingRow, 
              b::uint32 criteriaCount,
              bool assumeSingleMatch,
              std::shared_ptr<s::iresult> result);

    void readAll(std::shared_ptr<s::iresult> result);

    void readIds(std::shared_ptr<std::vector<std::shared_ptr<s::icell>>> criteriaRow,
                 std::shared_ptr<s::icolumn> idColumn,
                 std::vector<b::uint32>& ids);

    void readActiveIds(
        std::shared_ptr<std::vector<std::shared_ptr<s::icell>>> criteriaRow,
        std::shared_ptr<s::icolumn> idColumn,
        std::shared_ptr<s::icolumn> activeColumn,
        std::vector<b::uint32>& ids);

    void dispose(std::shared_ptr<std::vector<std::shared_ptr<s::icell>>> criteriaRow);

    void disposeInactiveRecords(
        std::shared_ptr<s::iindex> keyIndex,
        std::shared_ptr<std::vector<std::shared_ptr<s::icell>>> criteriaRow, 
        b::uint32 criteriaCount,
        bool assumeSingleMatch);

    void disposeAll();

private:
    std::weak_ptr<icommon_table_client> _client;
    std::shared_ptr<s::iengine> _dbEngine;

    std::wstring _name;
    std::shared_ptr<s::itable> _table;

    std::shared_ptr<s::iindex> _index;

    std::shared_ptr<std::vector<std::shared_ptr<s::icell>>> _row;
};

std::shared_ptr<icommon_table> createCommonTable(std::weak_ptr<icommon_table_client> _client, std::shared_ptr<s::iengine> dbEngine, std::wstring name);

}}} // journal::model::db